package com.jbh.netlibrary.u;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import java.io.IOException;


/**
 * 网络监听
 *
 * @author yxj on 2023/3/10
 */
public class NetStateUtils {

    /**
     * 获取当前的网络状态 ：没有网络-0：WIFI网络1：5G网络-5  4G网络-4：3G网络-3：2G网络-2
     */
    public static int getNetworkType(Context context) {
        int netType = 0;
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();

        if (networkInfo == null) {
            //不能上网
            return netType;
        }

        //以下都可以上网  不同的网络连接
        int nType = networkInfo.getType();
        switch (nType) {
            case ConnectivityManager.TYPE_WIFI:
                //WIFI
                netType = 1;
                break;
            case ConnectivityManager.TYPE_MOBILE:
                int nSubType = networkInfo.getSubtype();
                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                if (nSubType == TelephonyManager.NETWORK_TYPE_LTE && !telephonyManager.isNetworkRoaming()) {
                    //4G
                    netType = 4;
                } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                        || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                        || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                        && !telephonyManager.isNetworkRoaming()) {
                    //3G   联通的3G为UMTS或HSDPA 电信的3G为EVDO
                    netType = 3;
                } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                        || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                        || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                        && !telephonyManager.isNetworkRoaming()) {
                    //2G 移动和联通的2G为GPRS或EGDE，电信的2G为CDMA
                    netType = 2;
                } else {
                    netType = 5;
                }
                break;

            default: {
                netType = 6;
                break;
            }
        }
        return netType;
    }




    /**
     * @param context
     * @return
     */
    private static boolean isNetWork(Context context) {
        boolean b = false;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
            if (info != null) {
                for (NetworkInfo i : info) {
                    if (i.getState() == NetworkInfo.State.CONNECTED) {
                        b = true;
                        break;
                    }
                }
            }
        } else {
            NetworkInfo info = connectivityManager.getActiveNetworkInfo();
            b = info != null && info.getState() == NetworkInfo.State.CONNECTED;
        }
        return b;
    }


    /**
     * 检测网络是否可用   主要是检测网线
     *
     * @param context
     * @return
     */
    private static boolean ping(Context context) {
        // 通过ping百度检测WIFI是否可用
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        try {
            String ip = "www.baidu.com";// ping 的地址，可以换成任何一种可靠的外网
            Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);//
            int status = p.waitFor();// 只有0时表示正常返回。
            return (connectivityManager.getActiveNetworkInfo() != null && status == 0);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 网络是否可用
     *
     * @param context
     * @return
     */
    public static boolean isHaveNet(Context context) {
        boolean isNet = isNetWork(context);
        if (!isNet) {
            isNet = ping(context);
        }
        return isNet;
    }

}
